/*
 * @Author: 黄文杰
 * @Date: 2023-05-04 13:03:05
 * @LastEditors: 黄文杰
 * @LastEditTime: 2023-05-04 15:05:07
 * @FilePath: \ZNDS_V1xx\Bsp_Port\bsp_RingBuffer.c
 * @Description: 环形缓冲区处理函数
 *
 * Copyright (c) 2023 by 航信通科技有限公司, All Rights Reserved.
 */

#include "bsp_RingBuffer.h"
#include "bsp_port.h"

#define TIMESTAMP get_Timestampus()
/**
 * @description: 环形缓冲区存入一个数据
 * @param {RingBuffer_Typedef} *Rbuf 要操作的环形缓冲区
 * @param {unsigned char} data  环形缓冲区要存入的数据
 * @return {*} 无
 */
void RB_WriteByte(RingBuffer_Typedef *Rbuf, unsigned char data)
{
    Rbuf->RB_Data[Rbuf->RB_Index] = data;
    Rbuf->RB_Index++;
    Rbuf->timestamp = TIMESTAMP;
    Rbuf->BufEmpty = 0;

    if (Rbuf->RB_Index >= Rbuf->buf_size)
    {
        Rbuf->RB_Index = 0;
    }
}
/**
 * @description: 环形缓冲区读出n个数据
 * @param {RingBuffer_Typedef} *Rbuf 要操作的环形缓冲区
 * @param {unsigned char} *data 读出的数据缓冲区
 * @param {unsigned char} Datalen 要读取的字节个数
 * @return {*}
 */
unsigned int RB_ReadBytes(RingBuffer_Typedef *Rbuf, unsigned char *data, unsigned int Datalen)
{
    for (unsigned int i = 0; i < Datalen; i++)
    {
        if (Rbuf->RB_Index != Rbuf->RB_Outdex)
        {
            data[i] = Rbuf->RB_Data[Rbuf->RB_Outdex];
        }
        else
        {
            Rbuf->BufEmpty = 1;
            return i;
        }

        Rbuf->RB_Outdex++;

        if (Rbuf->RB_Outdex >= Rbuf->buf_size)
        {
            Rbuf->RB_Outdex = 0;
        }
    }

    return Datalen;
}
